﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DATN.mds.ContactManagement
{
    public partial class EditCM : System.Web.UI.Page
    {  
        string cnnString;
        SqlConnection cnn;
        SqlCommand cmd;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Load Dropdownlis
                Drr();
                if (Session["Cus_Id"] != null && Session["Us_Id"] != null)
                {
                    //BindCusTomer
                    BindCus();
                    //Note Remind
                    Session["Remind"] = null;

                }else{
                    Response.Redirect("../Account/Login.aspx");
                }
            }
        }

        //Bind data vào textbox
        protected void BindCus()
        {
            String cus = Session["Cus_Id"].ToString();
            try
            {
                cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn = new SqlConnection(cnnString);
                cnn.Open();

                String queryString = "SELECT * FROM [dbo].[Customers]  WHERE (([Cus_Id] = @Cus_Id))";
                cmd = new SqlCommand(queryString, cnn);
                cmd.Parameters.Add("@Cus_Id", SqlDbType.Int);
                cmd.Parameters["@Cus_Id"].Value = cus;

                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    TbxFirstName.Text = reader["FirstName"].ToString();
                    TbxLastName.Text = reader["LastName"].ToString();
                    TbxBirthday.Text = reader["Birthday"].ToString();
                    TbxPhone.Text = reader["Phone"].ToString();
                    TbxMail.Text = reader["Mail"].ToString();
                    TbxAddress.Text = reader["Address"].ToString();
                    TbxCompany.Text = reader["Company"].ToString();
                    TbxSalary.Text = reader["Salary"].ToString();
                    TbxHabits.Text = reader["Habit"].ToString();
                    ddp_sex.SelectedValue = reader["Sex"].ToString();
                    ddp_City.SelectedValue = reader["City"].ToString();
                    ddp_MaStatus.SelectedValue = reader["MaritalStatus"].ToString();
                    ddp_Job.SelectedValue = reader["Job"].ToString();
                    //ddp_dataviewCustomers.SelectedValue = reader["GrCus_Id"].ToString();
                }

                cnn.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex);
                cnn.Close();
            } 
        
        }

        //Click vao Button Save
        protected void BtSave_Click(object sender, EventArgs e)
        {
            if (Session["Cus_Id"] != null && Session["Us_Id"] != null)
            {
                try
                {
                    int cus = Convert.ToInt16(Session["Cus_Id"].ToString());
                    int us = Convert.ToInt16(Session["Us_Id"].ToString());
                    Session.Contents.Remove("Cus_Id");
                    //Session.Contents.Remove("Us_Id");

                    cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                    cnn = new SqlConnection(cnnString);
                    cnn.Open();
                    cmd = cnn.CreateCommand();
                    cmd.CommandText = "UPDATE [dbo].[Customers] SET [FirstName] = @FirstName,[LastName] = @LastName, "
                            + "[Birthday] = @Birthday, [Sex] = @Sex, [Phone] = @Phone, "
                            + "[Mail] = @Mail, [Address] = @Address, [City] = @City, "
                            + "[Company] = @Company, [MaritalStatus] = @MaritalStatus, [Job] = @Job, "
                            + "[Salary] = @Salary, [Habit] = @Habit, [DateCreate] = @DateCreate, "
                            + "[Us_Id] = @Us_Id WHERE (([Cus_Id] = @Cus_Id))";

                    cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50);
                    cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 50);
                    cmd.Parameters.Add("@Birthday", SqlDbType.Date);
                    cmd.Parameters.Add("@Sex", SqlDbType.NVarChar, 20);
                    cmd.Parameters.Add("@Phone", SqlDbType.VarChar, 16);
                    cmd.Parameters.Add("@Mail", SqlDbType.NVarChar, 50);
                    cmd.Parameters.Add("@Address", SqlDbType.NVarChar, 150);
                    cmd.Parameters.Add("@City", SqlDbType.NVarChar, 50);
                    cmd.Parameters.Add("@Company", SqlDbType.NVarChar, 50);
                    cmd.Parameters.Add("@MaritalStatus", SqlDbType.NVarChar, 20);
                    cmd.Parameters.Add("@Job", SqlDbType.NVarChar, 50);
                    cmd.Parameters.Add("@Salary", SqlDbType.Int);
                    cmd.Parameters.Add("@Habit", SqlDbType.NText);
                    cmd.Parameters.Add("@DateCreate", SqlDbType.DateTime);
                    cmd.Parameters.Add("@Us_Id", SqlDbType.Int);
                    cmd.Parameters.Add("@Cus_Id", SqlDbType.Int);

                    cmd.Parameters["@FirstName"].Value = TbxFirstName.Text;
                    cmd.Parameters["@LastName"].Value = TbxLastName.Text;
                    cmd.Parameters["@Birthday"].Value = TbxBirthday.Text;
                    cmd.Parameters["@Sex"].Value = ddp_sex.SelectedValue.ToString();
                    cmd.Parameters["@Phone"].Value = TbxPhone.Text;
                    cmd.Parameters["@Mail"].Value = TbxMail.Text;
                    cmd.Parameters["@Address"].Value = TbxAddress.Text;
                    cmd.Parameters["@City"].Value = ddp_City.SelectedValue.ToString();
                    cmd.Parameters["@Company"].Value = TbxCompany.Text;
                    cmd.Parameters["@MaritalStatus"].Value = ddp_MaStatus.SelectedValue.ToString();
                    cmd.Parameters["@Job"].Value = ddp_Job.SelectedValue.ToString();
                    cmd.Parameters["@Salary"].Value = TbxSalary.Text;
                    cmd.Parameters["@Habit"].Value = TbxHabits.Text.ToString();
                    cmd.Parameters["@DateCreate"].Value = DateTime.Now.ToString();
                    //cmd.Parameters["@GrCus_Id"].Value = Convert.ToInt16(ddp_dataviewCustomers.SelectedValue.ToString());
                    cmd.Parameters["@Us_Id"].Value = us;
                    cmd.Parameters["@Cus_Id"].Value = cus;

                    cmd.ExecuteNonQuery();

                    //Response.Write("Da Update thanh cong");
                    LbStatus.Text = "Cập Nhật Thành Công!";
                    cnn.Close();
                }
                catch (Exception ex)
                {
                    Response.Write(ex);
                }
            }
            else { 
                    Response.Redirect("../Account/Login.aspx"); 
            }
        }

        //Trờ lại trang Danh Bạ
        protected void BtCancel_Click(object sender, EventArgs e)
        {
            Session.Contents.Remove("Cus_Id");
            
            Response.Redirect("/mds.ContactManagement/IndexCM.aspx");
        }

        //Load DropDowlist
        public void Drr()
        {
            //gioi tinh
            var gioitinh = new Dictionary<string, string>();
            gioitinh.Add("1", "Nam");
            gioitinh.Add("2", "Nữ");
            gioitinh.Add("3", "Khác");

            ddp_sex.DataTextField = "Value";
            ddp_sex.DataValueField = "Key";
            ddp_sex.DataSource = gioitinh;
            ddp_sex.DataBind();

            //Tinh trang hon nhan
            var Masta = new Dictionary<string, string>();
            Masta.Add("1", "Độc Thân");
            Masta.Add("2", "Đã Kết Hôn");
            Masta.Add("3", "Khác");

            ddp_MaStatus.DataTextField = "Value";
            ddp_MaStatus.DataValueField = "Key";
            ddp_MaStatus.DataSource = Masta;
            ddp_MaStatus.DataBind();

            //Tinh trang hon nhan
            var City = new Dictionary<string, string>();
            City.Add("1", "Hà Nội");
            City.Add("2", "Hưng yên");
            City.Add("3", "Điện Biên");
            City.Add("4", "Nam Định");

            ddp_City.DataTextField = "Value";
            ddp_City.DataValueField = "Key";
            ddp_City.DataSource = City;
            ddp_City.DataBind();

            //Cong viec
            var congviec = new Dictionary<string, string>();
            congviec.Add("1", "Kinh doanh");
            congviec.Add("2", "Văn phòng");
            congviec.Add("3", "Kĩ thuật");
            congviec.Add("4", "Học Sinh, Sinh Viên");
            congviec.Add("5", "Nhà Nước");

            ddp_Job.DataTextField = "Value";
            ddp_Job.DataValueField = "Key";
            ddp_Job.DataSource = congviec;
            ddp_Job.DataBind();

            //------------------------------------------------------------------------------------
            //try
            //{
            //cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
            //cnn = new SqlConnection(cnnString);
            //SqlDataAdapter da = new SqlDataAdapter("select * from GroupCustomer", cnn);
            //DataSet dsm = new DataSet();
            //da.Fill(dsm, "testx");

          
            //    //===== Dropdownlist - SandBox=======================
            //    ddp_dataviewCustomers.DataSource = dsm.Tables["testx"].DefaultView;
            //    ddp_dataviewCustomers.DataTextField = "Name";
            //    ddp_dataviewCustomers.DataValueField = "GrCus_Id";
            //    ddp_dataviewCustomers.DataBind();
            //    cnn.Close();
            //}
            //catch (Exception ex)
            //{
            //    cnn.Close();
            //    Response.Write(ex);
            //}
        }
    }
}